//
//  SFGiftOtherCollectionViewCell.m
//  GiftThat
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFGiftOtherCollectionViewCell.h"
#import "SFGiftThatTableViewCell.h"
#import <MJRefresh.h>
@implementation SFGiftOtherCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.page = 0;
     
        self.backgroundColor = [UIColor whiteColor];
        [self GetTableView];
        self.dataSource = [NSMutableArray array];

    }
    return self;
}
-(void)setRow:(NSString *)row{
    if (_row != row) {
        _row = row;
    }

//    调解析数据的方法 如果在init方法调方法会不显示
     [self GetData];
//    刷新
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
   
}
//首页以外cell的数据解析

-(void)GetData{
      
    [CXNetWorkTool getWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=%ld&gender=1&generation=1",self.row,(long)self.page] parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        [self.dataSource removeAllObjects];
        self.GiftOtherCellModel = [result[@"data"] objectForKey:@"paging"];
        
        NSArray *array = [result[@"data"] objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            self.SelectionCellModel = [[SFSelectionCellModel alloc]initWithDictionary:dic];
            [self.dataSource addObject:self.SelectionCellModel];
         
        }
      
        [self.tableview reloadData];
    } fail:^(NSError *result) {
        
    }];
}
//创建ableView
- (void) GetTableView{
    self.tableview = [[UITableView alloc]initWithFrame:self.bounds  style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;   
    [self.tableview registerClass:[SFGiftThatTableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self addSubview:self.tableview];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(DidPress) name:@"Refresh" object:nil];
    
}
-(void)DidPress{
    [self.tableview reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     SFGiftThatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    if (self.dataSource.count > indexPath.row) {
    
        [cell setSelectionCellModel:self.dataSource[indexPath.row]];
    }
    
         return cell;
 
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.delegate tableviewOtherJump:[self.dataSource[indexPath.row] content_url]];
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.tableview.frame = CGRectMake(0, 0, self.width, self.height - 70);
}

-(void)loadNewData{
    [self.dataSource removeAllObjects];

     [self.tableview.mj_header endRefreshing];
    [self GetData];
    


}

-(void)loadMore{
    self.page += 20;
  
        
        [CXNetWorkTool getWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=%ld&gender=1&generation=1",self.row,(long)self.page] parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
          
            self.GiftOtherCellModel = [result[@"data"] objectForKey:@"paging"];
            
            NSArray *array = [result[@"data"] objectForKey:@"items"];
            for (NSDictionary *dic in array) {
                self.SelectionCellModel = [[SFSelectionCellModel alloc]initWithDictionary:dic];
                [self.dataSource addObject:self.SelectionCellModel];
                
            }
            
            [self.tableview reloadData];
        } fail:^(NSError *result) {
            
        }];
  

    [self.tableview.mj_footer endRefreshing];
}
@end
